package com.sea.finance.service.impl;

import com.codecrane.core.util.CountryCurrencyEnum;
import com.codecrane.core.util.CountryMoneyTypeEnum;
import com.sea.finance.entity.ExchangeRate;
import com.sea.finance.entity.ExchangeRateRecord;
import com.sea.finance.mapper.ExchangeRateMapper;
import com.sea.finance.service.ExchangeRateRecordService;
import com.sea.finance.service.ExchangeRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class ExchangeRateServiceImpl implements ExchangeRateService {

    @Autowired
    private ExchangeRateMapper exchangeRateMapper;
    @Autowired
    private ExchangeRateRecordService exchangeRateRecordService;

    @Override
    public List<ExchangeRate> findByCnd(ExchangeRate rate) {
        return exchangeRateMapper.findByCnd(rate);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(ExchangeRate rate) {
        rate.setRateToName(CountryCurrencyEnum.getCountry(Integer.parseInt(rate.getRateCountry())));
        rate.setRateName(CountryMoneyTypeEnum.getCountry(Integer.parseInt(rate.getRateCountry())));
        exchangeRateMapper.insert(rate);
        ExchangeRateRecord record=new ExchangeRateRecord();
        record.setRateId(rate.getId());
        record.setRatePercent(rate.getRatePercent());
        return exchangeRateRecordService.save(record);
    }

    @Override
    public ExchangeRate findById(Integer id) {
        return exchangeRateMapper.findById(id);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int usave(ExchangeRate rate) {
        exchangeRateMapper.update(rate);
        ExchangeRateRecord record=new ExchangeRateRecord();
        record.setRateId(rate.getId());
        record.setRatePercent(rate.getRatePercent());
        return exchangeRateRecordService.save(record);
    }

    @Override
    public ExchangeRate findByFromName(String rateFromName, String rateToName) {
        return exchangeRateMapper.findByFromName(rateFromName, rateToName);
    }

    @Override
    public ExchangeRate findByRateName(String rateFromName, String rateName) {
        return exchangeRateMapper.findByRateName(rateFromName, rateName);
    }
}
